using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace Risiko.Menu
{
  public interface ITransition
  {
    event MenuEvent OnStartGame;
  }

  public class Transition : BaseMenu, ITransition
  {

    Rectangle Start, CLRec, CRRec;
    Texture2D CL, CR;
    protected SpriteBatch spriteBatch;
    Button but1;
    int direction = 1;

    public Transition(Game game)
      : base(game)
    {
      TouchPanel.EnabledGestures = GestureType.Tap;
      background = game.Content.Load<Texture2D>(@"Images//Board//RisikoMap");
      bounds = new Rectangle(0, 0, 800, 480);
    }

    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {

      spriteBatch = this.Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

      Start = new Rectangle(bounds.Center.X, bounds.Center.Y, 100, 100);

      CL = Game.Content.Load<Texture2D>(@"Images//Board//ClankLeft");
      CR = Game.Content.Load<Texture2D>(@"Images//Board//ClankRight");

      CLRec = new Rectangle(-Game.GraphicsDevice.Viewport.Bounds.Width - 100, 0, Game.GraphicsDevice.Viewport.Bounds.Width + 100, Game.GraphicsDevice.Viewport.Bounds.Height);
      CRRec = new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Width, 0, Game.GraphicsDevice.Viewport.Bounds.Width + 100, Game.GraphicsDevice.Viewport.Bounds.Height);

      
      base.Initialize();
    }

    /// <summary>
    /// Allows the game component to update itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    /// 
    
    int phase1 = 1, phase2 = 2, currentPhase = 1, timer = 10;
    public override void Update(GameTime gameTime)
    {
      if(currentPhase == phase1)
       { 
          if (CRRec.X > bounds.X)
            CRRec.X += -(direction * 45);

          if (CLRec.X < bounds.X)
            CLRec.X += direction * 45;

          if (CRRec.X < 0 && CLRec.X > bounds.X - 5)
            {
            currentPhase = phase2;
            direction = -1;
            }
       }

      if(currentPhase == phase2)
       {
         timer -= 1;

         if (timer <= bounds.X)
          {

            if (CRRec.X <= GraphicsDevice.Viewport.Bounds.Width)
           CRRec.X += -(direction * 45);

            if (CLRec.X >= -GraphicsDevice.Viewport.Bounds.Width)
           CLRec.X += direction * 45;
          }

         if (CLRec.X <= -GraphicsDevice.Viewport.Bounds.Width / 2)
          {
            timer = 0;
          if (OnStartGame != null)
            OnStartGame();
          }

          }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {

      base.Draw(gameTime);

      spriteBatch.Begin();

      if (currentPhase == phase1)
      {
        spriteBatch.Draw(CL, new Rectangle(0, 0, 800, 480), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
        spriteBatch.Draw(CR, new Rectangle(0, 0, 800, 480), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
      }
      
      spriteBatch.Draw(CL, CLRec, Color.White);
      spriteBatch.Draw(CR, CRRec, Color.White);


      //but1.DrawCenteredButton(spriteBatch);            
      spriteBatch.End();
    }

    
    public event MenuEvent OnStartGame;
  }
}
